<?php

namespace App\Http\Controllers\Admin;
header('Content-Type:text/html;charset=utf-8');


use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Route,URL,Auth;


class BgController extends Controller
{



      //获取当前时间
      public function time (){
           date_default_timezone_set('Asia/Shanghai');
           $time= date('Y-m-d H:i:s',time());
           return $time;
      }

    public function addbg(Request $request){
        if($request->isMethod('post')){
            $input = $request->all();
            $data = Input::except('_token');


            $data = array(
                'bg_title' => $input['bg_title'],
                'bg_qy' => $input['bg_qy'],
                'bg_qydz' => $input['bg_qydz'],
                'bg_jyfw' => $input['bg_jyfw'],
                'bg_content' => $input['case_desc'],
                'bg_time' => $input['bg_time'],
                'add_time' =>  $this->time(),
                'bg_uid' => Auth::guard('admin')->user()->id,
 
            );
            foreach ($data as $k=>$v){
                $data[$k] = htmlspecialchars($v);
            }

            $abg = DB::table('bgt')->insert($data);
            if ($abg) {
                 return ['status'=>1,'msg'=>'添加成功'];
            }else{
                 return ['status'=>0,'msg'=>'添加失败'];
            }
            // return redirect('admin/bg/bgindex');
        }

         return view('admin.bg.addbg');
    }

    public function bgindex(){
        $res = DB::table('bgt')->orderBy('bg_time','desc')->paginate(10);
        $count = DB::table('bgt')->count();
        return view('admin.bg.bgindex',['res'=>$res,'count'=>$count]);

    }

    public function bgajax(request $request){
        if ($request) {
            $res = DB::table('bgt')->orderBy('bg_time','desc')->paginate($request->rows);
            $list = $res->items();

            foreach ($list as $k=>$v){
                $read = array(
                    'unit_id' => Auth::guard('admin')->user()->unit_id,
                    'bgt_id'=> $v->bg_id,
                );
                $readok = DB::table('readbgt')->where($read)->first();
                if (!$readok) {
                    $list[$k]->read = '0';
                }else{
                    $list[$k]->read = '1';
                }
            }

            $result['total'] = $res->total();
            $result['rows'] = $list;
            echo json_encode($result);
        }
    }

    public function edit($id,Request $request)
    {
        $re = DB::table('bgt')->where('bg_id',$id)->first();

        if($request->isMethod('post')){
            $input = $request->all();
             $data = array(

                'bg_title' => $input['bg_title'],
                'bg_qy' => $input['bg_qy'],
                'bg_qydz' => $input['bg_qydz'],
                'bg_jyfw' => $input['bg_jyfw'],
                'bg_content' => $input['case_desc'],
                'bg_time' => $input['bg_time'],
                'update_time' =>  $this->time(),
                'bg_uid' => Auth::guard('admin')->user()->id,
 
            );
            foreach ($data as $k=>$v){
                $data[$k] = htmlspecialchars($v);
            }
           $upbg = DB::table('bgt')->where('bg_id',$id)->update($data);
            if ($upbg) {
                 return ['status'=>1,'msg'=>'修改成功'];
            }else{
                 return ['status'=>0,'msg'=>'修改失败'];
            }

        }


        //判断企业是否已读
        if (Auth::guard('admin')->user()->role_id == 22) {
             $map = array(
                'unit_id' => Auth::guard('admin')->user()->unit_id,
                'bgt_id' => $id,
              );
             $findread = db::table('readbgt')->where($map)->first();
             if (!$findread) {
                $addread =  db::table('readbgt')->insert($map);
             }
        }



        return view('admin.bg.edit', ['re' => $re,'id'=>$id]);
    }

    public function del($id)
    {   
    
        $re = db::table('bgt')->where('bg_id',$id)->delete();
        if($re){
            return ['status'=>1,'msg'=>'删除成功'];
        }else{
            return ['status'=>0,'msg'=>'删除失败'];
        }
        return redirect()->back();

    }

    public function tj(){
         return view('admin.bg.tj');
    }

}
